<?php

/**
 * Export Category data to xml data 
 *
 * @category   Magestore
 * @package    Magestore_Mageconnector
 * @author     Nguyen Minh Dung (http://oss.com.vn)
 */
class Magestore_Mageconnector_Model_Category extends Magestore_Mageconnector_Model_Abstract {

    /**
     * Export Category to XML Data by store code
     *
     * @param string $baseUrl
     * @param string $storeCode
     * @return string
     */
    public function exportXML($baseUrl, $storeCode = null) {
        $store = $this->getStore($baseUrl, $storeCode);
        Mage::app()->setCurrentStore($store);
        $group = Mage::getModel('core/store_group')->load($store->getGroupId());
        $xml = '<?xml version="1.0"?><categories>';
        $category = Mage::getModel('catalog/category')
                        ->setStoreId($store->getId())
                        ->load($group->getRootCategoryId());

        if ($category instanceof Mage_Catalog_Model_Category) {
            $this->pushChildrenCategory($baseUrl, $category->getChildrenCategories(), $xml, $store);
        }
        $xml .= '</categories>';
        return $xml;
    }

    /**
     *
     * @param string $baseUrl
     * @param string $categoryCollection
     * @param string $xml
     * @param Mage_Core_Model_Store $store
     */
    private function pushChildrenCategory($baseUrl, $categoryCollection, &$xml, $store) {
        foreach ($categoryCollection as $category) {
            if ($category instanceof Mage_Catalog_Model_Category) {
                if ($category->getIsActive()) {
                    $totalProducts = count(
                            $category->setStoreId($store->getId())
                                    ->getProductCollection()
                                    ->addAttributeToFilter(
                                        array(
                                            array(
                                                'attribute' => 'visibility',
                                                'in' => array(2, 4)
                                            )
                                        )
                                    )
                    );
                    $urlKey = $category->getUrlKey();
                    
                    $xml .= '<category>'
                            . '<id>' . $category->getId() . '</id>'
                            . '<name><![CDATA[' . $category->getName() . ']]></name>'
                            . '<url_key><![CDATA[' . $urlKey . ']]></url_key>'
                            . '<url><![CDATA[' . $category->getUrl() . ']]></url>'
                            . '<total_products>' . $totalProducts . '</total_products>'
                            . '<children>';
                    $this->pushChildrenCategory($baseUrl, $category->getChildrenCategories(), $xml, $store);
                    $xml .= '</children></category>';
                }
            }
        }
    }

    public function getListActiveCategoryIdsByStoreView($baseUrl, $storeCode = null) {
        $store = $this->getStore($baseUrl, $storeCode);
        $group = Mage::getModel('core/store_group')->load($store->getGroupId());
        $category = Mage::getModel('catalog/category')
                        ->setStoreId($store->getId())
                        ->load($group->getRootCategoryId());

        $list = array();
        if ($category instanceof Mage_Catalog_Model_Category) {
            $this->pushActiveChildrenCategory($baseUrl, $category->getChildrenCategories(), $list, $store);
        }
        return $list;
    }

    private function pushActiveChildrenCategory($baseUrl, $categoryCollection, &$list, $store) {
        foreach ($categoryCollection as $category) {
            if ($category instanceof Mage_Catalog_Model_Category) {
                if ($category->getIsActive()) {
                    $list[] = $category->getId();
                    $this->pushActiveChildrenCategory($baseUrl, $category->getChildrenCategories(), $list, $store);
                }
            }
        }
    }

}